import os
import time

import pymysql
from PySide6 import QtWidgets, QtCore
from PySide6.QtCore import QObject, QEvent, Qt, QPoint, QUrl, Slot
from PySide6.QtGui import QKeyEvent, QTextCursor
from PySide6.QtWebChannel import QWebChannel
from PySide6.QtWebEngineWidgets import QWebEngineView
from PySide6.QtWidgets import QApplication, QMainWindow, QMessageBox, QComboBox, QListView, QListWidget, \
    QListWidgetItem, QTableWidget, QTableWidgetItem, QHeaderView, QPushButton, QTreeWidget, QTreeWidgetItem, QLineEdit, \
    QTabWidget, QPlainTextEdit, QLabel, QWidget, QHBoxLayout, QSizePolicy, QSpacerItem, QToolButton, QMenu
from PySide6.QtUiTools import QUiLoader

from controllers.data_list_fill_data import DataListFillData
from controllers.data_list_filter import DataListFilter
from controllers.data_list_page import DataListPage
from controllers.database_select import DatabaseSelect
from controllers.field_list_fill_data import FieldListFillData
from controllers.history_list_fill_data import HistoryListFillData
from controllers.loading import LoadingMask
from controllers.tab_sql_query import SqlQuery
from controllers.tab_table_information import TableInformation
from controllers.table_list import TableList
from global_variables import GlobalVariables


class Success(QMainWindow):
    def __init__(self, app):
        super().__init__()
        self.app = app
        app.success = self

        # 登录成功后
        self.ui = QUiLoader().load(GlobalVariables.PROJECT_BASE_PATH + 'resource/ui/model.ui')
        self.app.ui = self.ui

        # 数据库连接对象
        self.app.mysqldb = GlobalVariables.DATABASE_OBJ

        self.app.database_select = DatabaseSelect(self.app)
        self.app.data_list_filter = DataListFilter(self.app)
        self.app.data_list_fill_data = DataListFillData(self.app)
        self.app.data_list_page = DataListPage(self.app)
        self.app.field_list_fill_data = FieldListFillData(self.app)
        self.app.table_list = TableList(self.app)
        self.app.tab_table_information = TableInformation(self.app)
        self.app.tab_sql_query = SqlQuery(self.app)
        self.app.tab_sql_history = HistoryListFillData(self.app)

    # 初始化
    def init(self, default_database: str):
        list_column = self.app.mysqldb.collation_character_set_applicability()
        GlobalVariables.MAP_COLLECTION_CHARSET = {
            i['COLLATION_NAME']: i['CHARACTER_SET_NAME'] for i in list_column
        }
        self.app.database_select.selected_database_fill_data(default_database)
        self.ui.tab_table_widget.currentChanged.connect(self.tab_table_widget_current_changed)

        # 窗口最大化
        # self.ui.showMaximized()

        # 数据库支持的引擎列表
        list_engine = self.app.mysqldb.show_engines()
        GlobalVariables.LIST_DATABASE_ENGINE = [engine['Engine'] for engine in list_engine]


    # tab标签切换
    def tab_table_widget_current_changed(self, index: int):
        tab_widget = self.ui.tab_table_widget.currentWidget()
        if tab_widget.objectName() == 'tab_table_content':
            self.click_tab_table_content()
        elif tab_widget.objectName() == 'tab_table_struct':
            self.click_tab_table_struct()
        elif tab_widget.objectName() == 'tab_table_information':
            self.click_tab_table_information()
        elif tab_widget.objectName() == 'tab_sql_query':
            self.click_tab_sql_query()
        elif tab_widget.objectName() == 'tab_sql_history':
            self.click_tab_sql_history()

    def click_tab_table_content(self):
        item = self.ui.table_list.currentItem()
        if item is None:
            return
        # 如果表名不发生变化，不动数据
        table_name = item.text()
        data_list: QTableWidget = self.ui.data_list
        if table_name == data_list.property("table_name"):
            return
        self.app.data_list_fill_data.first_data_list_fill_data(table_name=table_name)

    def click_tab_table_struct(self):
        item = self.ui.table_list.currentItem()
        if item is None:
            return
        table_name = item.text()
        self.app.field_list_fill_data.fill_data(table_name)

    def click_tab_table_information(self):
        item = self.ui.table_list.currentItem()
        if item is None:
            return
        table_name = item.text()
        self.app.tab_table_information.tab_table_information_fill_data(table_name)

    def click_tab_sql_query(self):
        pass

    def click_tab_sql_history(self):
        self.app.tab_sql_history.add_history_to_view()


if __name__ == '__main__':
    pass
